<?php

class GraphController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        // content="text/plain; charset=utf-8"
        require_once ('jpgraph/src/jpgraph.php');
        require_once ('jpgraph/src/jpgraph_bar.php');
        require_once ('jpgraph/src/jpgraph_line.php');
    }
    
    public function preDispatch() 
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            if ($this->_request->getActionName() != 'login') {
                $this->_redirect('/user/login');
            }
        }
    }
    public function graph1Action()
    {
        $stockdate = new Application_Model_StockDate();
        $calrisk = new Application_Model_CalRisk();
        $result1 = $calrisk->fetchAll('ret_12m > -100', 'ret_12m DESC', 10);
        $this->view->result1 = $result1;
        
        $data1 = array();
        $data2 = array();
        $data3 = array();
        foreach($result1 as $d)
        {
            $data1[] = ((($d->_mRet12m)-0.08) / (($d->_mVol12m)*sqrt(250)));
            $data2[] = $d->_mRet12m * 100;
            $data3[] = $d->_mVol12m * sqrt(250) * 100;
        }
        
        // Create the graph. These two calls are always required
        $graph = new Graph(450,180);
        $graph->SetScale("textlin", 0, 6);
        $graph->SetY2Scale("lin",0,300);
        $graph->SetY2OrderBack(false);
        
        //$theme_class="DefaultTheme";
        //$graph->SetTheme(new $theme_class());
        
        // set major and minor tick positions manually
        $graph->yaxis->SetTickPositions(array(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0), array(.5, 1.5, 2.5, 3.5, 4.5, 5.5));
        $graph->y2axis->SetTickPositions(array(0, 0, 100, 200, 300));
        $graph->SetBox(false);
        
        //$graph->ygrid->SetColor('gray');
        $graph->ygrid->SetFill(false);
        //$graph->xaxis->SetTickLabels(array('A','B','C','D'));
        $graph->yaxis->HideLine(false);
        $graph->yaxis->HideTicks(false,false);
        
        // Create the bar plots
        $b1plot = new BarPlot($data1);
        $p1 = new LinePlot($data2);
        $p2 = new LinePlot($data3);
        
        // ...and add it to the graPH
        $graph->Add($b1plot);
        $graph->AddY2($p1);
        $graph->AddY2($p2);
        
        $b1plot->SetColor("green");
        $b1plot->SetLegend('Sharpe Ratio');
        $b1plot->SetFillGradient("green","green",GRAD_LEFT_REFLECTION);
        $b1plot->SetWidth(15);
        
        $p1->SetColor("purple");
        $p1->SetLegend('Lợi nhuận');
        $p1->mark->SetType(MARK_FILLEDCIRCLE,'',1.0);
        $p1->mark->SetColor('purple');
        $p1->mark->SetFillColor('purple');
        $p1->SetCenter();
        
        $p2->SetColor("blue");
        $p2->SetLegend('Độ lệch chuẩn');
        $p2->mark->SetType(MARK_FILLEDCIRCLE,'',1.0);
        $p2->mark->SetColor('blue');
        $p2->mark->SetFillColor('blue');
        $p2->SetCenter();
        
        // Display the graph
        $graph->Stroke();
        $this->view->graph = $graph;
        
    }
    public function graph2Action()
    {
        $stockdate = new Application_Model_StockDate();
        $calrisk = new Application_Model_CalRisk();
        $where = array();
        $result2 = $calrisk->fetchAll($where, 'von_thi_truong DESC', 10);
        $this->view->result2 = $result2;
        
        $data1 = array();
        $data2 = array();
        $data3 = array();
        foreach($result2 as $d)
        {
            $data1[] = $d->_mVonThiTruong;
            $data2[] = $d->_mRet12m * 100;
            $data3[] = $d->_mVol12m * sqrt(250) * 100;
        }
        
        // Create the graph. These two calls are always required
        $graph = new Graph(450,180);
        $graph->SetScale("textlin", 0, 70000);
        $graph->SetY2Scale("lin",-100,300);
        $graph->SetY2OrderBack(false);
        
        //$theme_class="DefaultTheme";
        //$graph->SetTheme(new $theme_class());
        
        // set major and minor tick positions manually
        $graph->yaxis->SetTickPositions(array(0, 10000, 20000, 30000, 40000, 50000, 60000, 70000), array(5000, 15000, 25000, 35000, 45000, 55000, 65000));
        $graph->y2axis->SetTickPositions(array(-100, 0, 100, 200, 300));
        $graph->SetBox(false);
        
        //$graph->ygrid->SetColor('gray');
        $graph->ygrid->SetFill(false);
        //$graph->xaxis->SetTickLabels(array('A','B','C','D'));
        $graph->yaxis->HideLine(false);
        $graph->yaxis->HideTicks(false,false);
        
        // Create the bar plots
        $b1plot = new BarPlot($data1);
        $p1 = new LinePlot($data2);
        $p2 = new LinePlot($data3);
        
        // ...and add it to the graPH
        $graph->Add($b1plot);
        $graph->AddY2($p1);
        $graph->AddY2($p2);
        
        $b1plot->SetColor("green");
        $b1plot->SetLegend('Sharpe Ratio');
        $b1plot->SetFillGradient("green","green",GRAD_LEFT_REFLECTION);
        $b1plot->SetWidth(15);
        
        $p1->SetColor("purple");
        $p1->SetLegend('Lợi nhuận');
        $p1->mark->SetType(MARK_FILLEDCIRCLE,'',1.0);
        $p1->mark->SetColor('purple');
        $p1->mark->SetFillColor('purple');
        $p1->SetCenter();
        
        $p2->SetColor("blue");
        $p2->SetLegend('Độ lệch chuẩn');
        $p2->mark->SetType(MARK_FILLEDCIRCLE,'',1.0);
        $p2->mark->SetColor('blue');
        $p2->mark->SetFillColor('blue');
        $p2->SetCenter();
        
        // Display the graph
        $graph->Stroke();
        $this->view->graph = $graph;
    }
    public function graph3Action()
    {
        $stockdate = new Application_Model_StockDate();
        $calrisk = new Application_Model_CalRisk();
        $where = array();
        $result2 = $calrisk->fetchAll($where, 'von_thi_truong DESC', 10);
        $this->view->result2 = $result2;
        
        $data1 = array();
        $data2 = array();
        $data3 = array();
        foreach($result2 as $d)
        {
            $data1[] = $d->_mVonThiTruong;
            $data2[] = $d->_mRet12m * 100;
            $data3[] = $d->_mVol12m * sqrt(250) * 100;
        }
        
        // Create the graph. These two calls are always required
        $graph = new Graph(450,180);
        $graph->SetScale("textlin", 0, 70000);
        $graph->SetY2Scale("lin",-100,300);
        $graph->SetY2OrderBack(false);
        
        //$theme_class="DefaultTheme";
        //$graph->SetTheme(new $theme_class());
        
        // set major and minor tick positions manually
        $graph->yaxis->SetTickPositions(array(0, 10000, 20000, 30000, 40000, 50000, 60000, 70000), array(5000, 15000, 25000, 35000, 45000, 55000, 65000));
        $graph->y2axis->SetTickPositions(array(-100, 0, 100, 200, 300));
        $graph->SetBox(false);
        
        //$graph->ygrid->SetColor('gray');
        $graph->ygrid->SetFill(false);
        //$graph->xaxis->SetTickLabels(array('A','B','C','D'));
        $graph->yaxis->HideLine(false);
        $graph->yaxis->HideTicks(false,false);
        
        // Create the bar plots
        $b1plot = new BarPlot($data1);
        $p1 = new LinePlot($data2);
        $p2 = new LinePlot($data3);
        
        // ...and add it to the graPH
        $graph->Add($b1plot);
        $graph->AddY2($p1);
        $graph->AddY2($p2);
        
        $b1plot->SetColor("green");
        $b1plot->SetLegend('Sharpe Ratio');
        $b1plot->SetFillGradient("green","green",GRAD_LEFT_REFLECTION);
        $b1plot->SetWidth(15);
        
        $p1->SetColor("purple");
        $p1->SetLegend('Lợi nhuận');
        $p1->mark->SetType(MARK_FILLEDCIRCLE,'',1.0);
        $p1->mark->SetColor('purple');
        $p1->mark->SetFillColor('purple');
        $p1->SetCenter();
        
        $p2->SetColor("blue");
        $p2->SetLegend('Độ lệch chuẩn');
        $p2->mark->SetType(MARK_FILLEDCIRCLE,'',1.0);
        $p2->mark->SetColor('blue');
        $p2->mark->SetFillColor('blue');
        $p2->SetCenter();
        
        // Display the graph
        $graph->Stroke();
        $this->view->graph = $graph;
    }
    public function graph4Action()
    {
        $stockdate = new Application_Model_StockDate();
        $calrisk = new Application_Model_CalRisk();
        $where = array();
        $result4 = $calrisk->fetchAll($where, 'volume_12m DESC', 10);
        $this->view->result4 = $result4;
        
        $data1 = array();
        $data2 = array();
        $data3 = array();
        foreach($result4 as $d)
        {
            $data1[] = $d->_mVolume12m;
            $data2[] = $d->_mRet12m * 100;
            $data3[] = $d->_mVol12m * sqrt(250) * 100;
        }
        
        // Create the graph. These two calls are always required
        $graph = new Graph(450,180);
        $graph->SetScale("textlin");
        $graph->SetY2Scale("lin",-100,200);
        $graph->SetY2OrderBack(false);
        
        //$theme_class="DefaultTheme";
        //$graph->SetTheme(new $theme_class());
        
        // set major and minor tick positions manually
        $graph->yaxis->SetTickPositions(array(-100, 0, 1000000, 2000000, 3000000, 4000000), array(500000, 1500000, 2500000, 3500000));
        $graph->y2axis->SetTickPositions(array(-100, 0, 100, 200));
        $graph->SetBox(false);
        
        //$graph->ygrid->SetColor('gray');
        $graph->ygrid->SetFill(false);
        //$graph->xaxis->SetTickLabels(array('A','B','C','D'));
        $graph->yaxis->HideLine(false);
        $graph->yaxis->HideTicks(false,false);
        
        // Create the bar plots
        $b1plot = new BarPlot($data1);
        $p1 = new LinePlot($data2);
        $p2 = new LinePlot($data3);
        
        // ...and add it to the graPH
        $graph->Add($b1plot);
        $graph->AddY2($p1);
        $graph->AddY2($p2);
        
        $b1plot->SetColor("green");
        $b1plot->SetLegend('Sharpe Ratio');
        $b1plot->SetFillGradient("green","green",GRAD_LEFT_REFLECTION);
        $b1plot->SetWidth(15);
        
        $p1->SetColor("purple");
        $p1->SetLegend('Lợi nhuận');
        $p1->mark->SetType(MARK_FILLEDCIRCLE,'',1.0);
        $p1->mark->SetColor('purple');
        $p1->mark->SetFillColor('purple');
        $p1->SetCenter();
        
        $p2->SetColor("blue");
        $p2->SetLegend('Độ lệch chuẩn');
        $p2->mark->SetType(MARK_FILLEDCIRCLE,'',1.0);
        $p2->mark->SetColor('blue');
        $p2->mark->SetFillColor('blue');
        $p2->SetCenter();
        
        // Display the graph
        $graph->Stroke();
        $this->view->graph = $graph;
    }
}